package com.hyzx.qbasic.admin.domain.service.impl;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.SysConfigPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.AddConfigReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.DeleteConfigReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.EditConfigReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetConfigListReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.common.util.OperationResUtils;
import com.hyzx.qbasic.admin.domain.repository.ISysConfigRepository;
import com.hyzx.qbasic.admin.domain.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 黄江华 on 2018/7/20
 */

@Service
public class SysConfigServiceImpl implements ISysConfigService {
    @Autowired
    private ISysConfigRepository configRepository;

    @Override
    public Map<String, String> getAllConfig() {
        List<SysConfigPO> configPOList = configRepository.getAllConfigList();
        Map<String, String> configMap = new HashMap<>(configPOList.size());

        for (SysConfigPO configPO : configPOList) {
            configMap.put(configPO.getConfKey(), configPO.getConfValue());
        }

        return configMap;
    }

    @Override
    public PageInfo<SysConfigPO> getPagedConfigList(GetConfigListReq req) {
        PageInfo<SysConfigPO> configList = configRepository.getPagedConfigList(
                req.getPageNum(), req.getPageSize(), req.getConfKey());

        return configList;
    }

    @Override
    public BaseOperationRes addConfig(AddConfigReq req) {
        //检查配置是否已存在
        SysConfigPO existedConfig = configRepository.getConfigInfoByKey(req.getConfKey());
        if (existedConfig != null)
            return OperationResUtils.fail("配置已存在");

        SysConfigPO configPO = new SysConfigPO();
        configPO.setConfKey(req.getConfKey());
        configPO.setConfValue(req.getConfValue());
        configPO.setRemark(req.getRemark());
        configPO.setCreateTime(new Date());
        configPO.setUpdateTime(new Date());

        boolean success = configRepository.addConfig(configPO);
        if (!success)
            return OperationResUtils.fail("新增配置失败");

        return OperationResUtils.success();
    }

    @Override
    public BaseOperationRes deleteConfig(DeleteConfigReq req) {
        boolean success = configRepository.deleteConfig(req.getConfIdList());
        if (!success)
            return OperationResUtils.fail("删除配置失败");

        return OperationResUtils.success();
    }

    @Override
    public SysConfigPO getConfigInfoById(int confId) {
        return configRepository.getConfigInfoById(confId);
    }

    @Override
    public String getConfigByKey(String confKey) {
        SysConfigPO configPO = configRepository.getConfigInfoByKey(confKey);
        if (configPO == null) {
            return null;
        }
        return configPO.getConfValue();
    }

    @Override
    public BaseOperationRes editConfig(EditConfigReq req) {
        //检查配置是否存在
        SysConfigPO existedConfig = configRepository.getConfigInfoById(req.getConfId());
        if (existedConfig == null)
            return OperationResUtils.fail("配置不存在");

        SysConfigPO configPO = new SysConfigPO();
        configPO.setConfId(req.getConfId());
        configPO.setConfKey(req.getConfKey());
        configPO.setConfValue(req.getConfValue());
        configPO.setRemark(req.getRemark());
        configPO.setUpdateTime(new Date());

        boolean success = configRepository.editConfig(configPO);
        if (!success)
            return OperationResUtils.fail("编辑配置失败");

        return OperationResUtils.success();
    }
}
